//
//  ProThemeViewController.m
//  wedding
//
//  Created by 常宽 on 15/1/28.
//  Copyright (c) 2015年 常宽. All rights reserved.
//

#import "ProThemeViewController.h"
#import "ThemeModel.h" ///传进来的model的类
#import "MyFlowLayout.h"    /// 自定义的flowlayout
#import "ThemeGoodsModel.h" /// 自定义model
#import "ThemeDescCollectionViewCell.h" ///自定义集合视图描述cell
#import "ThemeCollectionViewCell.h" /// 自定义集合视图图文混合cell
#import "ProGoodsViewController.h"  /// 下页,商品详情页

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height


@interface ProThemeViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
/// 数据数组
@property (nonatomic, retain)NSMutableArray *dataArray;
/// 集合视图
@property (nonatomic, retain)UICollectionView *collectionView;
@end

@implementation ProThemeViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.dataArray = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = self.model.title;
    // 二级页面隐藏tabBar
    self.tabBarController.tabBar.hidden = YES;
    // 重写返回上级页面方法
    UIImage *backImage = [[UIImage imageNamed:@"返回.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem= [[UIBarButtonItem alloc] initWithImage:backImage style:UIBarButtonItemStylePlain target:self action:@selector(popBack:)];
    [self netWorking];
    [self createCollection];
}
/// 网络请求
- (void)netWorking
{
    NSString *string = [NSString stringWithFormat:@"http://hunliji.com/subjects/%@.json?user_id=(null)", self.model.idNumder];
    [NetHandler networkWithURL:string completion:^(id result) {
        NSArray *array = [result objectForKey:@"products"];
        for (NSDictionary *dic in array) {
            ThemeGoodsModel *model = [[ThemeGoodsModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        
        [self.collectionView reloadData];
        
    }];
}

/// 返回上级页面
- (void)popBack:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 创建集合视图及协议
// 创建集合视图
- (void)createCollection
{
    MyFlowLayout *flowLayout = [[MyFlowLayout alloc]init];
    
    UIImageView *imageView = [[UIImageView alloc]init];
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.model.bkg_path]];
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:flowLayout];
    
    [self.collectionView registerClass:[ThemeCollectionViewCell class] forCellWithReuseIdentifier:@"theme"];
    [self.collectionView registerClass:[ThemeDescCollectionViewCell class] forCellWithReuseIdentifier:@"themeDesc"];
    
    [self.collectionView setBackgroundView:imageView];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.view addSubview:self.collectionView];
}
// 返回多少个section数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
// 每个section上返回的item数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return self.dataArray.count;
            break;
        default:
            return 0;
            break;
    }
}
// 返回cell的样式
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
            ThemeDescCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"themeDesc" forIndexPath:indexPath];
            cell.layer.cornerRadius = 10;
            cell.clipsToBounds = YES;
            cell.backgroundColor = [[WebColor crimson]colorWithAlphaComponent:0.3];
            cell.model = self.model;
            return cell;
        }
            break;
        case 1:
        {
            ThemeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"theme" forIndexPath:indexPath];
            cell.backgroundColor = [[WebColor lightPink]colorWithAlphaComponent:0.3];
            cell.layer.cornerRadius = 10;
            cell.clipsToBounds = YES;
            ThemeGoodsModel *model = self.dataArray[indexPath.item];
            cell.model = model;
            return cell;
        }
            break;
        default:
        {
            ThemeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"xxxx" forIndexPath:indexPath];
            cell.backgroundColor = [[WebColor lightPink]colorWithAlphaComponent:0.3];
            cell.layer.cornerRadius = 10;
            cell.clipsToBounds = YES;
            ThemeGoodsModel *model = self.dataArray[indexPath.item];
            cell.model = model;
            return cell;
        }
            break;
    }
}
// 点击cell触发的事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 1:
        {
            ProGoodsViewController *pro = [[ProGoodsViewController alloc] init];
            pro.model = self.dataArray[indexPath.item];
            [self.navigationController pushViewController:pro animated:YES];
        }
            break;
        default:
            break;
    }
}
#pragma mark -









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
